package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsUserAccountMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.pojo.DtsUser;
import com.qf.pojo.DtsUserAccount;
import com.qf.service.DtsUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;


@Service
public class DtsUsersServiceImpl implements DtsUsersService {

@Autowired
    DtsUserMapper userMapper;
@Autowired
DtsUserAccountMapper accountMapper;
    @Override
    public IPage<DtsUser> queryPage(Integer page, Integer limit, String sort, String order, String username, String mobile){
        IPage<DtsUser> ipage = new Page(page, limit);
        QueryWrapper<DtsUser> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(username)) {
            qw.like("username", username);
        }
        if(!StringUtils.isEmpty(mobile)) {
            qw.like("mobile", mobile);
        }
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if(order.equalsIgnoreCase("desc")) {
                qw.orderByDesc(sort);
            } else {
                qw.orderByAsc(sort);
            }
        }

        userMapper.selectPage(ipage, qw);
        return ipage;
    }

    @Override
    public void updateSettlement(DtsUserAccount userAccount) {
        Integer id=userAccount.getUserId();
        DtsUser dtsUser = userMapper.selectById(id);
        dtsUser.setUserLevel(2);
        dtsUser.setStatus(0);
        userMapper.updateById(dtsUser);
        LambdaQueryWrapper<DtsUserAccount> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsUserAccount::getUserId, id);
        userAccount.setModifyTime(new Date());
        accountMapper.update(userAccount,qw);
    }
}
